package com.gtq.uhome.controller;

import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.MarkiService;
import com.gtq.uhome.vo.DeliveryOrderManagementVo;
import com.gtq.uhome.vo.MarkiVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 配送员管理控制层
 */
@CrossOrigin
@Controller
@RequestMapping("/marki")
@ResponseBody
@Api(tags = "总后台，配送员管理模块接口说明文档，田小凡")
public class MarkiController {

    @Resource
    MarkiService markiService = null;

    /**
     * 配送员管理，查询所有
     * @return 配送员集合
     */
    @GetMapping
    @ApiOperation(value = "配送员管理，查询所有配送员信息",response = Map.class,httpMethod = "GET")
    public Result<List<MarkiVo>> findAll(){
        List<MarkiVo> all = markiService.findAll();
        return Result.ok(all);
    }

    /**
     * 模糊查询
     * @return
     */
    @PostMapping("/findLike")
    @ApiOperation(value = "配送员管理，模糊查询所有配送员信息",notes = "姓名",response = Map.class,httpMethod = "POST")
    @ApiImplicitParam(name = "markiVo",value = "配送员对象",dataType = "String",required = true)
    public Result<List<MarkiVo>> findLike(@RequestBody MarkiVo markiVo){
        List<MarkiVo> findLike = markiService.findLike(markiVo);

        return Result.ok(findLike);
    }

    /**
     * 添加配送员
     * @param markiVo
     * @return
     */
    @PostMapping
    @ApiOperation(value = "配送员管理，添加配送员",response = Map.class,httpMethod = "POST")
    @ApiImplicitParam(name = "markiVo",value = "配送员对象")
    public Result insertMarki(@RequestBody MarkiVo markiVo){
        //判断电话号码是否重复
        Boolean isRepeat = markiService.isRepeat(markiVo.getStaffPhoneNumber());
        if(isRepeat){
            //重复
            return Result.failed("手机号码已存在！");
        }else {
            //不重复
            Boolean isInsert = markiService.insertMarki(markiVo);
            if(isInsert){
                return Result.ok("添加成功");
            }else {
                return Result.failed("添加失败！请稍后重试！");
            }
        }

    }

    /**
     * 删除配送员
     * @param staffId
     * @return
     */
    @PostMapping("/deleteMarki")
    @ApiOperation(value = "配送员管理,删除配送员",response = Map.class,httpMethod = "POST")
    @ApiImplicitParam(name = "stuffId",value = "配送员对象的id")
    public Result deleteMarki(@RequestBody List<MarkiVo> staffId){

        Boolean isDelete = markiService.deleteMarki(staffId);
        if(isDelete){
            return Result.ok("删除成功！");
        }else {
            return Result.failed("删除失败！");
        }
    }

    /**
     * 修改状态，是否冻结，0：未冻结，1：冻结
     * @param staffId
     * @return
     */
    @PostMapping("/freeze")
    @ApiOperation(value = "配送员管理设置配送员状态,冻结",response = Map.class,httpMethod = "POST")
    @ApiImplicitParam(name = "freeze",value = "冻结")
    public Result freeze(@RequestBody List<MarkiVo> staffId){
        System.out.println(staffId);
        Boolean isFreeze = markiService.freeze(staffId);
        if(isFreeze){
            return Result.ok("操作成功！");
        }else {
            return Result.ok("操作失败！");
        }

    }

    /**
     * 修改状态，是否冻结，0：未冻结，1：冻结
     * @param staffId
     * @return
     */
    @PostMapping("/notFreeze")
    @ApiOperation(value = "配送员管理设置配送员状态，解冻",response = Map.class,httpMethod = "POST")
    @ApiImplicitParam(name = "freeze",value = "正常")
    public Result notFreeze(@RequestBody List<MarkiVo> staffId){

        Boolean isFreeze = markiService.notFreeze(staffId);
        if(isFreeze){
            return Result.ok("操作成功！");
        }else {
            return Result.ok("操作失败！");
        }

    }

    /**
     * 修改配送员信息
     * @param markiVo
     * @return
     */
    @PutMapping
    @ApiOperation(value = "配送员管理,修改配送员信息",response = Map.class,httpMethod = "PUT")
    @ApiImplicitParam(name = "stuffId",value = "配送员id")
    public Result updateMarki(@RequestBody MarkiVo markiVo){
        Boolean isUpdate = markiService.updateMarki(markiVo);
        if(isUpdate){
            return Result.ok("修改成功！");
        }else {
            return Result.failed("修改失败！");
        }
    }



    /**
     * 派送列表 --> 快递员查询
     * ----李龙
     * 查询派送员
     */
    @GetMapping("/select")
    @ApiOperation(value = "站点后台-->派送列表  查询接口 有问题咨询李龙", notes = "使用GET请求访问", response = Map.class, httpMethod = "GET")
    public Result fandAllMarki(){
        List<MarkiVo> markiVos = markiService.fandAllMarki();
        if (markiVos!=null)
            return Result.ok(markiVos,"查询成功");
        else
            return Result.failed("没有查询到派送员");
    }

    /**
     * 通过模糊查询派送员
     * 通过派送员姓名和手机号查询
     * ----李龙
     * @param map 用于存储字段的对象
     * @return 返回结果
     */
    @PostMapping("/vagueFind")
    @ApiOperation(value = "站点后台-->派送列表 快递员选择模糊查询接口 有问题咨询李龙", notes = "使用GET请求，请求时带有一下其中字段vagueFand:用户输入值", response = Map.class, httpMethod = "GET")
    public Result vagueFindAllMarki(@RequestBody Map map){
        String  vagueFind = (String) map.get("vagueFind");
        System.out.println("派送员模糊查询："+vagueFind);
        //调用service层模糊查询
        List<MarkiVo> markiVos = markiService.vagueFandAllMarki(vagueFind);
        //如果有值，则返回查询结果
        if (markiVos.size()>0)
            return Result.ok(markiVos,"查询成功");
        return Result.failed("没有查询到数据");
    }

    /**
     * 平台派单方法
     *
     * ----李龙
     * @param markiVo 用于存储字段的对象
     * @return 返回结果
     */
    @PostMapping("/platformDispatch")
    @ApiOperation(value = "站点后台-->派送列表 平台派单方法 有问题咨询李龙", notes = "使用POST请求，请求时带有一下其中字段courierId：派送员id，orderId：订单id", response = Map.class, httpMethod = "POST")
    public Result platformDispatch(@RequestBody Map<String ,Object> markiVo){
        Integer courierId = (Integer) markiVo.get("courierId");
        String orderId = (String) markiVo.get("orderId");

        Boolean aBoolean = markiService.platformDispatch(courierId, orderId);
        //如果有值，则返回查询结果
        if (aBoolean)
            return Result.ok(null,"平台派单成功");
        return Result.failed("派单失败，请稍后再试");
    }

    /**
     * 查询未派单
     *
     * ----李龙
     *
     * @return 返回结果
     */
    @GetMapping("/selectUndelivered")
    @ApiOperation(value = "站点后台-->派送列表 查询未派单 有问题咨询李龙", notes = "查询全部未派单的数据", response = Map.class, httpMethod = "GET")
    public Result selectUndelivered(){
        List<DeliveryOrderManagementVo> deliveryOrderManagements = markiService.selectUndelivered();
        //如果有值，则返回查询结果
        if (deliveryOrderManagements!=null)
            return Result.ok(deliveryOrderManagements,"查询未派送订单成功");
        return Result.failed("暂时没有订单，请稍后再试");
    }

    /**
     * 未派送订单模糊查询
     *
     * ----李龙
     *
     * @return 返回结果
     */
    @PostMapping("/vagueSelectUndelivered")
    @ApiOperation(value = "站点后台-->派送列表 未派送订单模糊查询：需要字段如下：vagueSelect：用户输入的值 有问题咨询李龙", notes = "根据订单号和收货人姓名和收货人电话号码模糊查询", response = Map.class, httpMethod = "POST")
    public Result vagueSelectUndelivered(@RequestBody Map<String,String> map ){
        String vagueSelect = map.get("vagueSelect");
        System.out.println("模糊查询字段:"+vagueSelect);
        List<DeliveryOrderManagementVo> deliveryOrderManagementVos = markiService.vagueSelectUndelivered(vagueSelect);
        if (deliveryOrderManagementVos.size()>0)
            return Result.ok(deliveryOrderManagementVos,"查询成功");
        return Result.failed("没有查询到订单");
    }

    /**
     * 派单详情方法
     *
     * ----李龙
     *
     * @return 返回结果
     */
    @PostMapping("/orderInfo")
    @ApiOperation(value = "站点后台-->派送列表 平台派送详情：需要字段如下：orderId：订单ID，courierId：配送员ID 有问题咨询李龙", notes = "平台派单，派单详情", response = Map.class, httpMethod = "POST")
    public Result DeliveryOrderDetails(@RequestBody Map orderInfo){
        String orderId = (String) orderInfo.get("orderId");
        Integer courierId = (Integer) orderInfo.get("courierId");

        System.out.println("订单Id"+orderId+"配送员ID"+courierId);
        Map map = markiService.orderInfo(orderId, courierId);
        if (map!=null){
            return Result.ok(map);
        }
        return Result.failed("系统原因，联系客服");
    }


}
